import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:timeago/timeago.dart' as timeago;
import 'package:zootix/constants/zt_cache_key.dart';
import 'package:zootix/services/zt_app_info.service.dart';
import 'package:zootix/services/zt_dio.service.dart';
import 'package:zootix/services/zt_purchase.service.dart';
import 'package:zootix/services/zt_storage.service.dart';
import 'package:zootix/services/zt_translate.service.dart';
import 'store/zt_store.dart';

/// 全局注册
class ZtGlobal {
  static Future init() async {
    WidgetsFlutterBinding.ensureInitialized();

    /// 设置初始化竖屏
    await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

    /// 设置系统UI 因用户反馈可能存在bug先隐藏功能
    setSystemUi();

    /// 初始化 几天前 的时区国际化
    timeago.setLocaleMessages('en', timeago.EnMessages());

    /// 依赖注入翻译服务
    await Get.putAsync<ZtTranslateService>(() => ZtTranslateService().init());

    /// 依赖注入持久化
    await Get.putAsync<ZtStorageService>(() => ZtStorageService().init());

    /// 依赖注入Http
    await Get.putAsync<ZtDioService>(() => ZtDioService().init());

    /// 依赖注入app信息
    await Get.putAsync<ZtAppInfoService>(() => ZtAppInfoService().init());

    /// 依赖注入购买服务
    await Get.putAsync<ZtPurchaseService>(() => ZtPurchaseService().init());

    /// 依赖注入用户信息
    await Get.putAsync<ZtAuthStore>(() => ZtAuthStore().init());

    if (ZtStorageService.to.getString(ZtCacheKeys.token, isWithUser: false) !=
        null) {
      /// 依赖注入IMChat
      await Get.putAsync<ZtImChartStore>(() => ZtImChartStore().init());

      /// 依赖注入票务信息
      Get.putAsync<ZtTicketStore>(() => ZtTicketStore().init());

      /// 依赖注入拉黑屏蔽信息
      Get.putAsync<ZtBlockStore>(() => ZtBlockStore().init());
    }
  }

  static void setSystemUi() {
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.light,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}
